﻿package com.TCPServer
{
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	/**
	 * TCP Server class to establish simple connections defining a port and Address to get any kind of data
	 * ...
	 * @author Omar Contreras
	 */
	public class TCPServer extends EventDispatcher
	{
		private static var timeToAsk:Timer;
		private static var _command:ByteArray;
		private static var tcpObject:TCPServerSocket;
		
		private var socketServerCommand:int;
		
		public function TCPServer():void 
		{
			
		}
		
		public function init(host:String = 'localhost', port:int = 1007) 
		{
			_command = new ByteArray();
			
			tcpObject = new TCPServerSocket(host, port);
			tcpObject.addEventListener(TCPServerSocketEvent.ONDATA, onData);
			tcpObject.addEventListener(TCPServerSocketEvent.ONCONNECT, onConnect);
			
		}
		
		public function sendCommandAutomatically(timeInterval:int = 10, socketServerCommand:int = -1):void 
		{
			this.socketServerCommand = socketServerCommand;
			
			timeToAsk = new Timer(timeInterval);
			timeToAsk.addEventListener(TimerEvent.TIMER, onTimer);
			timeToAsk.start();
		}
		
		private function onConnect(e:TCPServerSocketEvent):void 
		{
			//trace("connected!");
			//sendCommand();
			sendCommand(TCPServerSocket.SEND_STRING, "Connection on TCP OK");
		}
		
		private function onData(e:TCPServerSocketEvent):void 
		{
			e.data.buffer.position = 0;
			var firstByte:int = int(e.data.first);
			var secondByte:int = int(e.data.second);
			proccessIncommingData(firstByte, secondByte, e.data.buffer);
		}
		
		private function proccessIncommingData(_firstByte:int, _secondByte:int, _buffer:ByteArray):void 
		{
			//trace("Data Arrived!");
			_buffer.position = 0;
			
			if (_firstByte == 127 && _secondByte == 127)
			{
				var message:String = _buffer.readUTFBytes(_buffer.length);
				sendCommand(TCPServerSocket.SEND_STRING, "\r\nSTRING_ARRIVED");
				this.dispatchEvent(new TCPServerEvent(TCPServerEvent.STRING_ARRIVED, message));
			}
			else if (_firstByte == 2 && _secondByte == 1)
			{
				var valueInt:Number = _buffer.readDouble();
				sendCommand(TCPServerSocket.SEND_STRING, "INT_ARRIVED");
				this.dispatchEvent(new TCPServerEvent(TCPServerEvent.NUMBER_ARRIVED, valueInt));
			}
			else if (_firstByte == 3 && _secondByte == 1)
			{
				var valueFloat:Number = _buffer.readFloat();
				sendCommand(TCPServerSocket.SEND_STRING, "FLOAT_ARRIVED");
				this.dispatchEvent(new TCPServerEvent(TCPServerEvent.NUMBER_ARRIVED, valueFloat));
			}
			else if (_firstByte == 4 && _secondByte == 1)
			{
				sendCommand(TCPServerSocket.SEND_STRING, "BUFFER_ARRIVED");
				this.dispatchEvent(new TCPServerEvent(TCPServerEvent.BUFFER_ARRIVED, _buffer));
			}
		}
		
		public function sendCommand(type:int, obj:*):void 
		{
			tcpObject.sendCommand(type, obj);
		}
		
		private function onTimer(e:TimerEvent):void 
		{
			_command.clear();
			_command.writeByte(socketServerCommand);
			//tcpObject.sendCommand(_command);
		}
		
	}

}